export class playerInfo {
    uid = null
    name = null
    value = null
    prepared = 0
    grabRes = -1
    handCards = []
    outCards = []
}

import * as _ from 'lodash'
import { ShuffleCards, sortCards } from '../pukeData'

export class roomInfo{
    players = {}
    timeto = null
    dzid = null
    dzCard
    lastOut = null // {outStyle, uid}
    dealTask(json){
        switch(json.task){
            case 'joinPlayer':     return  this.joinPlayer(json.data); 
            case 'preparePlayer':  return  this.preparePlayer(json.data); 
            case 'grabCard':       return  this.grabCard(json.data); 
            case 'playOut':        return  this.playOut(json.data); 
            case 'quit':           return  this.quit(json.data); 
        }
    }
    joinPlayer(data){
        var uid = data.uid
        this.players[uid] = new playerInfo()
        
        return new Promise((res, rej)=>{
            res({ event:'joinPlayer', roomInfo: this })
        })
    }
    preparePlayer(data){
        var uid = data.uid
        this.players[uid].prepared = true
        var prepareNum = 0
        Object.keys(this.players).map(uid=>{
            if(this.players[uid].prepared) prepareNum += 1
        })
        if(prepareNum==3){
            var shuffled_cards = ShuffleCards()
            var cnt = 0
            Object.keys(this.players).map(uid=>{
                this.players[uid].handCards = sortCards(shuffled_cards.slice(cnt*17, (cnt+1)*17))
                cnt++
            })
            this.timeto = Object.keys(this.players)[Math.floor(Math.random()*3)]
            this.dzCard = shuffled_cards.slice(51,54)
            return new Promise((res, rej)=>{
                res({ event:"handOutCards", roomInfo: this })
            })
        }
        return new Promise((res, rej)=>{
            res({ event: "preparePlayer", roomInfo: this })
        })
    }
    nextPlayer(){
        var playerKeys = Object.keys(this.players)
        this.timeto = playerKeys[(playerKeys.indexOf(this.timeto)+1)%3]
        return new Promise((res, rej)=>{
            res({ event: "nextPlayer", roomInfo: this })
        })
    }
    grabCard(data){
        var uid = data.uid
        this.players[uid].grabRes = data.param.grabRes
        var grabCnt = 0
        Object.keys(this.players).map(uid=>{
            if(this.players[uid].grabRes!=-1) grabCnt++
        })
        this.nextPlayer()
        if(grabCnt==3){
            while(this.players[this.timeto].grabRes==0) this.nextPlayer()
            this.dzid = this.timeto
            this.players[this.dzid].handCards = sortCards(this.players[this.dzid].handCards.concat(this.dzCard))
            return new Promise((res, rej)=>{
                res({ event: "grabCardRes", roomInfo: this })
            })
        }
        return new Promise((res, rej)=>{
            res({ event: "grabCard", roomInfo: this })
        })
    }
    playOut(data){
        var uid = data.uid
        var param = data.param
        if(param.outStyle){
            var idList = param.outCards.map(item=>item.id)
            _.remove(this.players[uid].handCards, item=>idList.indexOf(item['id'])>=0)
            this.lastOut = {
                outStyle: param.outStyle, uid
            }
        }
        this.players[uid].outCards = param.outCards
        this.nextPlayer()
        if(this.lastOut && this.dzid==this.lastOut.uid){
            this.lastOut = null
        }
        if(this.players[uid].handCards.length==0){
            return new Promise((res, rej)=>{
                res({ event: "gameOver", roomInfo: this })
            })
        }
        return new Promise((res, rej)=>{
            res({ event: "playOut", roomInfo: this })
        })
    }
    quit(data){
        var uid = data.uid
        delete this.players[uid]
        return new Promise((res, rej)=>{
            res({ event: "quit", roomInfo: this })
        })
    }
}
